// ordered_array.c -- Implementation for creating, inserting and deleting
//                    from ordered arrays.
//                    Written for JamesM's kernel development tutorials.

#include "headers/ordered_array.h"
#include "../api/headers/dataTypes.h"
#include "../api/headers/string.h"
#include "../api/headers/errors.h"

s8int standard_lessthan_predicate(type_t a, type_t b)
{
    return (a<b)?1:0;
}

ordered_array_t create_ordered_array(u32int max_size, lessthan_predicate_t less_than)
{
    ordered_array_t to_ret;
    to_ret.array = (void*)kmalloc(max_size * sizeof(type_t));
    memset(to_ret.array, 0, max_size * sizeof(type_t));
    to_ret.size = 0;
    to_ret.max_size = max_size;
    to_ret.less_than = less_than;
    return to_ret;
}

ordered_array_t place_ordered_array(void *addr, u32int max_size, lessthan_predicate_t less_than)
{
    ordered_array_t to_ret;
    to_ret.array = (type_t*)addr;
    memset(to_ret.array, 0, max_size * sizeof(type_t));
    to_ret.size = 0;
    to_ret.max_size = max_size;
    to_ret.less_than = less_than;
    return to_ret;
}

void destroy_ordered_array(ordered_array_t *array)
{
    kfree(array->array);
}

void insert_ordered_array(type_t item, ordered_array_t *array)
{
	setLastError(NO_ERROR);
    ASSERT(array->less_than);
	if(array->size == array->max_size)
	{
		K_PANIC("Heap index overflow");
		setLastError(ERR_HEAP_INDEX_OVERFLOW);
		return;
	}
		
    u32int iterator = 0;
    while (iterator < array->size && array->less_than(array->array[iterator], item))
        iterator++;
    if (iterator == array->size) // just add at the end of the array.
        array->array[array->size++] = item;
    else
    {
        type_t tmp = array->array[iterator];
        array->array[iterator] = item;
        while (iterator < array->size)
        {
            iterator++;
            type_t tmp2 = array->array[iterator];
            array->array[iterator] = tmp;
            tmp = tmp2;
        }
        array->size++;
    }
}

// void update_ordered_array(u32int i, ordered_array_t array)
// {
// 	ASSERT(array->less_than);
// 	u32int iterator = 0;
// 	s32int direction = 1; //1 or -1
// 	
// 	if(((i == 0) && (array->less_than(array->array[i], array->array[i + 1])) || 
// 		((i == array->size - 1) && (array->less_than(array->array[i-1],array->array[i]))))
// 	{
// 		return; //Nothing changed. (if first or last element)
// 	}
// 	
// 	//Determin direction
// 	if(i == 0)
// 		dircetion = 1;
// 	else if(i == array->size - 1)
// 		direction = -1;
// 	else if(array->less_than(array->array[i], array[i - 1]))
// 		direction = -1;
// 	else
// 		direction = 1;
// 	
// 	//while((i > 0) && (i < 	//Bubble sort up or down.
// 	
// }

type_t lookup_ordered_array(u32int i, ordered_array_t *array)
{
    ASSERT(i < array->size);
    return array->array[i];
}

void remove_ordered_array(u32int i, ordered_array_t *array)
{
	setLastError(NO_ERROR);
	if(!array->size)
	{
		setLastError(ERR_IS_EMPTY);
		return;
	}
	if(i >= array->size)
	{
		setLastError(ERR_ELEMENT_NOT_FOUND);
		return;
	}
	
    while (i < array->size)
    {
        array->array[i] = array->array[i+1];
        i++;
    }
    array->size--;
}
